package br.com.calc.cliente;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import br.com.calc.cliente.model.ParametroSomaCliente;
import br.com.calc.cliente.model.ResultadoCliente;

import com.google.gson.Gson;

public class CalculadoraCliente {
	public static void main(String[] args) {

		 soma();
//		subtrair();

	}

	private static void subtrair() {
		Integer v1 = 3008;
		Integer v2 = 398;

		HttpClient client = new DefaultHttpClient();
		HttpPost request;
		try {
			request = new HttpPost(
					"http://localhost:8080/JAX-RS/calc/subtrair3");

			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
			nameValuePairs.add(new BasicNameValuePair("a", String.valueOf(v1)));
			nameValuePairs.add(new BasicNameValuePair("b", String.valueOf(v2)));
			request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			
			HttpResponse response = client.execute(request);

			String resultado = "";

			// Get the response
			BufferedReader rd = new BufferedReader(new InputStreamReader(
					response.getEntity().getContent()));

			String line = "";
			while ((line = rd.readLine()) != null) {
				resultado += line;
			}
			
			System.out.println(resultado);
			
		} catch (URISyntaxException e) {
			e.printStackTrace();
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static void soma() {
//		Integer v1 = 3;
//		Integer v2 = 398;

		HttpClient client = new DefaultHttpClient();
//		HttpGet request;
		HttpPost request;
		try {
//			request = new HttpGet("http://localhost:8080/JAX-RS/calc/somaJson/"
//					+ v1 + "/" + v2);
			
			Gson gson = new Gson();
			ParametroSomaCliente param = new ParametroSomaCliente(23, 76);
			
			String json = gson.toJson(param);
			
//			request = new HttpGet("http://localhost:8080/JAX-RS/calc/somaJson/" + json);
			request = new HttpPost("http://localhost:8080/JAX-RS/calc/somaJson/");
			
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
			nameValuePairs.add(new BasicNameValuePair("json", json));
			request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			
//			request.addHeader("accept", "application/json");

			HttpResponse response = client.execute(request);

			String resultado = "";

			// Get the response
			BufferedReader rd = new BufferedReader(new InputStreamReader(
					response.getEntity().getContent()));

			String line = "";
			while ((line = rd.readLine()) != null) {
				resultado += line;
			}

//			System.out.println(resultado);
			
			ResultadoCliente resultadoModel = gson.fromJson(resultado, ResultadoCliente.class);
			System.out.println(resultadoModel.getResultado());
			

		} catch (URISyntaxException e) {
			e.printStackTrace();
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
